#include <string.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n.h>

#include "sig_handle.h"
#include "dialogs.h"
#include "global_vars.h"

static void tips_dialog_response_handle(GtkWidget *tips_dialog,gint response_id,GtkWidget *notebook);
static gint tips_number;

void creat_settings_dialog()
{
	GtkWidget *settings_dialog;
	GtkWidget *bras_ip_lable;
	GtkWidget *bras_ip_entry;
	GtkWidget *hbox_1;
	GtkWidget *bras_offcampus_ip_label;
	GtkWidget *bras_offcampus_ip_entry;
	GtkWidget *hbox_2;
	GtkWidget *message_dialog;

	GIOChannel *ip_adress_read_channel;
	GIOChannel *ip_adress_write_channel;
	GString *bras_ip_adress;
	GString *bras_offcampus_ip_adress;
	//gsize ip_adress_length;

	ip_adress_read_channel=g_io_channel_new_file("./config/bras_ip","r",NULL);
	bras_ip_adress=g_string_new("");
	bras_offcampus_ip_adress=g_string_new("");

	g_io_channel_read_line_string(ip_adress_read_channel,bras_ip_adress,NULL,NULL);
	bras_ip_adress->str[bras_ip_adress->len-1]='\0';
	g_io_channel_read_line_string(ip_adress_read_channel,bras_offcampus_ip_adress,NULL,NULL);
	bras_offcampus_ip_adress->str[bras_offcampus_ip_adress->len-1]='\0';
	//g_print("%s",bras_ip_adress);
	settings_dialog=gtk_dialog_new_with_buttons(_("Settings"),NULL,GTK_DIALOG_NO_SEPARATOR,\
																			  _("OK"),GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,\
																			  NULL);
	gtk_window_set_resizable(GTK_WINDOW(settings_dialog),FALSE);
	gtk_window_set_icon_from_file(GTK_WINDOW(settings_dialog),"./image/configure.png",NULL);

	hbox_1=gtk_hbox_new(TRUE,0);
	bras_ip_lable=gtk_label_new(_("BRAS IP Address: "));
	bras_ip_entry=gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(bras_ip_entry),bras_ip_adress->str);
	gtk_entry_set_max_length(GTK_ENTRY(bras_ip_entry),IP_MAX_LENGTH);
	gtk_box_pack_start(GTK_BOX(hbox_1),bras_ip_lable,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(hbox_1),bras_ip_entry,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(settings_dialog)->vbox),hbox_1,FALSE,FALSE,0);

	hbox_2=gtk_hbox_new(TRUE,0);
	bras_offcampus_ip_label=gtk_label_new(_("BRAS IP Address (off campus): "));
	bras_offcampus_ip_entry=gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(bras_offcampus_ip_entry),bras_offcampus_ip_adress->str);
	gtk_entry_set_max_length(GTK_ENTRY(bras_offcampus_ip_entry),IP_MAX_LENGTH);
	gtk_box_pack_start(GTK_BOX(hbox_2),bras_offcampus_ip_label,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(hbox_2),bras_offcampus_ip_entry,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(settings_dialog)->vbox),hbox_2,FALSE,FALSE,0);

	gtk_widget_show_all(settings_dialog);

	switch(gtk_dialog_run(GTK_DIALOG(settings_dialog)))
	{
		case GTK_RESPONSE_CANCEL:
				break;
		case GTK_RESPONSE_OK:
				if (strcmp(bras_ip_adress->str,gtk_entry_get_text(GTK_ENTRY(bras_ip_entry)))==0&&\
					strcmp(bras_offcampus_ip_adress->str,gtk_entry_get_text(GTK_ENTRY(\
					bras_offcampus_ip_entry)))==0)
					break;

				bras_ip_adress=g_string_assign(bras_ip_adress,gtk_entry_get_text(GTK_ENTRY(bras_ip_entry)));
				bras_ip_adress=g_string_append_c(bras_ip_adress,'\n');
				bras_offcampus_ip_adress=g_string_assign(bras_offcampus_ip_adress,gtk_entry_get_text(\
																						GTK_ENTRY(bras_offcampus_ip_entry)));
				bras_offcampus_ip_adress=g_string_append_c(bras_offcampus_ip_adress,'\n');
				message_dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_NO_FLAG,\
																					  GTK_MESSAGE_WARNING,GTK_BUTTONS_YES_NO,
																					  _("Are you sure to modify the IP adress of the BRAS?"));
				gtk_widget_show_all(message_dialog);
				switch(gtk_dialog_run(GTK_DIALOG(message_dialog)))
				{
					case GTK_RESPONSE_YES:
						ip_adress_write_channel=g_io_channel_new_file("./config/bras_ip","w+",NULL);
						g_io_channel_write_chars(ip_adress_write_channel,bras_ip_adress->str,-1,NULL,NULL);
						g_io_channel_write_chars(ip_adress_write_channel,bras_offcampus_ip_adress->str,-1,\
																   NULL,NULL);
						g_io_channel_close(ip_adress_write_channel);
						break;
					case GTK_RESPONSE_NO:
						break;
				}
				gtk_widget_destroy(message_dialog);

				break;
	}

	gtk_widget_destroy(settings_dialog);
	g_io_channel_close(ip_adress_read_channel);
	g_string_free(bras_ip_adress,TRUE);
	g_string_free(bras_offcampus_ip_adress,TRUE);
}

void creat_watch_log_dialog()
{
	GtkWidget *watch_log_dialog;
	GtkWidget *watch_log_scrolled_window;
	GtkWidget *watch_log_text_view;
	GtkTextBuffer *log_text_view_buffer;

	GIOChannel *log_text_read_channel;
	gchar *log_text;


	log_text_read_channel=g_io_channel_new_file(LOG_FILE_PATH,"r",NULL);
	g_io_channel_read_to_end(log_text_read_channel,&log_text,NULL,NULL);

	watch_log_dialog=gtk_dialog_new_with_buttons(_("Logs"),NULL,\
																				   GTK_DIALOG_NO_SEPARATOR,\
																				  _("OK"),GTK_RESPONSE_OK,NULL);
	gtk_widget_set_size_request(watch_log_dialog,1000,600);
	gtk_window_set_icon_from_file(GTK_WINDOW(watch_log_dialog),"./image/diary.png",NULL);

	watch_log_scrolled_window=gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(watch_log_scrolled_window),\
														GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(watch_log_dialog)->vbox),watch_log_scrolled_window,TRUE,\
									TRUE,0);

	watch_log_text_view=gtk_text_view_new();
	log_text_view_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(watch_log_text_view));
	gtk_text_buffer_set_text(log_text_view_buffer,log_text,-1);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(watch_log_text_view),FALSE);
	gtk_container_add(GTK_CONTAINER(watch_log_scrolled_window),watch_log_text_view);

	gtk_widget_show_all(watch_log_dialog);
	gtk_dialog_run(GTK_DIALOG(watch_log_dialog));
	gtk_widget_destroy(watch_log_dialog);
	g_io_channel_close(log_text_read_channel);
}

void creat_tips_dialog()
{
	GtkWidget *tips_dialog;

	GtkWidget *image;
	GtkWidget *label;
	PangoFontDescription *label_font;
	GtkWidget *hbox;

	GtkWidget *notebook;
	GtkWidget **text_views;
	GtkTextBuffer *text_buffer;

	GIOChannel *tips_read_channel;
	gchar *tips;

	gint index;

	FILE *wc_result_stream;

	wc_result_stream=popen("cat ./config/tips| wc -l","r");

	if ((fscanf(wc_result_stream,"%d",&tips_number)==EOF)||(tips_number<=0))
		return;

	tips_dialog=gtk_dialog_new_with_buttons(_("Tips"),GTK_WINDOW(window_global),\
																		GTK_DIALOG_DESTROY_WITH_PARENT|\
																		GTK_DIALOG_NO_SEPARATOR,_("Close"),\
																		GTK_RESPONSE_CLOSE,_("Previous"),\
																		GTK_RESPONSE_PREVIOUS,\
																		_("Next"),GTK_RESPONSE_NEXT,NULL);

	hbox=gtk_hbox_new(FALSE,0);
	image=gtk_image_new_from_file("./image/info.png");
	label=gtk_label_new(PROMPT_WORDS);
	label_font=pango_font_description_from_string(FONT_OF_PROMPT);
	gtk_widget_modify_font(label,label_font);
	gtk_box_pack_start(GTK_BOX(hbox),image,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,0);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(tips_dialog)->vbox),hbox,FALSE,FALSE,0);

	notebook=gtk_notebook_new();
	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook),FALSE);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(tips_dialog)->vbox),notebook,TRUE,TRUE,0);

	text_views=g_malloc(sizeof(GtkWidget *)*tips_number);
	tips_read_channel=g_io_channel_new_file("./config/tips","r",NULL);

	for(index=0;index<tips_number;index++)
	{
		text_views[index]=gtk_text_view_new();
		gtk_text_view_set_editable(GTK_TEXT_VIEW(text_views[index]),FALSE);
		gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_views[index]),GTK_WRAP_WORD_CHAR);
		text_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_views[index]));
		g_io_channel_read_line(tips_read_channel,&tips,NULL,NULL,NULL);
		gtk_text_buffer_set_text(text_buffer,tips,-1);
		g_free(tips);
		gtk_widget_set_size_request(text_views[index],100,100);
		gtk_notebook_append_page(GTK_NOTEBOOK(notebook),text_views[index],NULL);
	}

	gtk_widget_show_all(tips_dialog);

	g_signal_connect(G_OBJECT(tips_dialog),"response",G_CALLBACK(\
								  tips_dialog_response_handle),notebook);

	g_io_channel_close(tips_read_channel);
}

static void tips_dialog_response_handle(GtkWidget *tips_dialog,gint response_id,GtkWidget *notebook)
{
	gint current_page;

	switch (response_id)
	{
		case GTK_RESPONSE_CLOSE:
			gtk_widget_destroy(tips_dialog);
			break;
		case GTK_RESPONSE_NEXT:
			current_page=gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
			current_page=(current_page+1)%tips_number;
			gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook),current_page);
			break;
		case GTK_RESPONSE_PREVIOUS:
			current_page=gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
			current_page=(current_page-1)%tips_number;
			gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook),current_page);
			break;
	}
}
